#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int countValidSelections(vector<int>& nums) {
        int n = nums.size();
        vector<int> left(n, 0);
        vector<int> right(n, 0);
        for (int i = 1; i < n; ++i) {
            left[i] = left[i - 1] + nums[i - 1];
        }
        for (int i = n - 2; i >= 0; --i) {
            right[i] = right[i + 1] + nums[i + 1];
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 0) {
                if (abs(left[i] - right[i]) == 1)
                    ++ans;
                else if (left[i] == right[i])
                    ans += 2;
            }
        }
        return ans;
    }
};


int main() {
    Solution sol;
    vector<int> nums = { 1,0,2,0,3 };
    cout << sol.countValidSelections(nums);
}